package com.yms.juc;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import org.springframework.util.StringUtils;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @version : 1.0
 * @author: yangmingsong
 * @date: 2022/7/8-07-08-9:29
 * @description: spring5Test 异步执行多线程并且能够返回值
 */
public class SysThreadReturnValueTest {


    public static void main(String[] args) {
        //现在模拟从数据库中查询两个表的数据，对数据进行合并并且排序的这么一个操作
        long start = System.currentTimeMillis();
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        try {

            CompletableFuture completableFuture = CompletableFuture.supplyAsync(new Task1(), threadPool);
            CompletableFuture completableFuture1 = completableFuture.thenCombineAsync(CompletableFuture.supplyAsync(new Task2(), threadPool), new SummarizeResult(), threadPool);
            JSONObject result = JSONObject.parseObject(String.valueOf(completableFuture1.join()));
            System.out.println(result);
            Task1 task1 = new Task1();
            String s = task1.get();
            Task2 task2 = new Task2();
            String s1 = task2.get();
            SummarizeResult summarizeResult = new SummarizeResult();
            JSONObject apply = summarizeResult.apply(s, s1);
            System.out.println(apply);


        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            threadPool.shutdown();
        }

        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

}

class Task1 implements Supplier {
    /**
     * 函数式接口用来处理执行的任务
     *
     * @return
     */
    @Override
    public String get() {
        System.out.println(Thread.currentThread().getName());
        return "33453453";
    }
}

class Task2 implements Supplier {
    /*
     *
     * 函数式接口用来处理执行的任务
     * @return
     */
    @Override
    public String get() {
        System.out.println(Thread.currentThread().getName());
        return "ddfgdfgbr";
    }
}

class ExceTest implements Function<Throwable, Exception> {
    /*
     * 函数式接口用来处理异常
     * @param throwable 异常
     * @return 返回异常
     */
    @Override
    public Exception apply(Throwable throwable) {
        throwable.getStackTrace();
        System.out.println("出现异常" + throwable.getMessage());
        return (Exception) throwable;
    }
}

class Result implements BiConsumer<String, Exception> {
    /*

     *
     * 函数式接口 用来处理结果
     * @param value 这是返回值
     * @param e  对应的异常
     */
    @Override
    public void accept(String value, Exception e) {
        if (e == null) {
            System.out.println("结果====" + value);
        }
    }
}

class SummarizeResult implements BiFunction {

    /*
     * 函数式接口
     * 合并两个任务的结果
     * @param value1 第一个任务返回的值
     * @param value2 第二个任务返回的值
     * @return 最终返回处理这两个任务的值
     */
    @Override
    public JSONObject apply(Object value1, Object value2) {
        System.out.println(Thread.currentThread().getName());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("value1", value1);
        jsonObject.put("value2", value2);
        return jsonObject;
    }
}

